package com.maimang.platform.crm.config;

import com.maimang.platform.crm.interceptor.MethodCacheInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname WebConfig
 * @description web配置api接口访问，为移动端提供接口
 * @date 2019-04-23 18:55
 * @email jintaozhao@qq.com
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
	// 不需要登录拦截的url:登录注册和验证码
	private final String[] notLoginInterceptPaths = {"/signin", "/login/**", "/register/**", "/captcha/**"};
	// 核心线程池大小
	private int corePoolSize = 5;
	// 最大可创建的线程数
	private int maxPoolSize = 20;
	// 队列最大长度
	private int queueCapacity = 20;
	// 线程池维护线程所允许的空闲时间
	private int keepAliveSeconds = 300;
	// 方法缓存拦截器
	@Autowired
	private MethodCacheInterceptor methodCacheInterceptor;

	/**
	 * 异步线程池
	 *
	 * @return
	 */
	@Bean
	public ThreadPoolTaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(corePoolSize);
		taskExecutor.setMaxPoolSize(maxPoolSize);
		taskExecutor.setQueueCapacity(queueCapacity);
		taskExecutor.setThreadNamePrefix("webAsyncTask-");
		return taskExecutor;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// jwt接口授权拦截器
		// registry.addInterceptor(methodCacheInterceptor).addPathPatterns("/**");
		// 登录拦截器
	}

	/**
	 * 跨域支持
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("*")
				.allowCredentials(true)
				.allowedMethods("GET", "POST", "DELETE", "PUT")
				.maxAge(3600 * 24);
	}
}
